<?php
namespace app\service;
use think\facade\Db;
use app\model\SingleModel;
use Exception;

class SingleService{  
    public function list($params){
        $where=[];
        $page = $params['page'];
        $pageCount = $params['page_count'];

        //按出发日期查询
        if(!empty($params['start_date']) && !empty($params['end_date'])){
            if(!empty($params['start_date'])){
                $where[] = ['a.start_date','>= time',$params['start_date']];  
            }
            if(!empty($params['end_date'])){
                $where[] = ['a.start_date','<= time',$params['end_date']];  
            }
        }  

        if(!empty($params['typeid'])){
            $where[] = ['a.type','=',$params['typeid']];   
        }
        if(!empty($params['keyword'])){
            $where[] = ['a.content|a.payment_no|a.sn','like',"%{$params['keyword']}%"];   
        }
        
        if(!empty($params['sn'])){
            $where[] = ['a.sn','=',$params['sn']];  
        }
        if(!empty($params['client_id'])){
            $where[] = ['a.client_id','=',$params['client_id']];  
        }
        if(!empty($params['supplier_id'])){
            $where[] = ['a.supplier_id','=',$params['supplier_id']];  
        }
       
        if(!empty($params['branch_id'])){
            $where[] = ['a.branch_id','=',$params['branch_id']];  
        }
        if(!empty($params['own_id'])){
            $where[] = ['a.own_id','=',$params['own_id']];  
        }
        if(!empty($params['operator_id'])){
            $where[] = ['a.operator_id','=',$params['operator_id']];  
        }

        
        $data = SingleModel::alias('a')
            ->field('a.*,b.title as branch,c.company_name as client,d.supplier_name as supplier,e.cn_name as operator,f.title as own')
            ->join('category b','a.branch_id=b.id','left')           
            ->join('client c','a.client_id=c.id','left')
            ->join('supplier d','a.supplier_id=d.id','left')           
            ->join('user e','a.operator_id=e.id','left')
            ->join('category f','a.own_id=f.id','left')
            ->where($where)->page($page,$pageCount)->order('id','desc')->select();
        foreach ($data as &$v) {  
            $monthly = Db::name("monthly")->where("year_month",date("Y-m",strtotime($v['start_date'])))->find();  
            if($monthly){
            $v['is_monthly'] =  1;
            }else{
            $v['is_monthly'] =  0;
            }

            $v['receipts'] = Db::name('receipt')->where(['type'=>$v['type'],'income_id'=>$v['id']])->select();  
            foreach ($v['receipts'] as $r) {
                if($r['is_review']){
                    $v['receipt_review']=1;
                }
            }
            $v['payments'] = Db::name('payment')->where(['type'=>$v['type'],'outlay_id'=>$v['id']])->select(); 
            foreach ($v['payments'] as $r) {
                if($r['is_review']){
                    $v['payment_review']=1;
                }
            }
        }
        $count = SingleModel::alias('a')->where($where)->count(); 
        return ["list"=>$data,"count"=>$count];
    } 
    
    public function info($id){
        $singles = SingleModel::alias('a')
            ->field('a.*,b.title as branch,c.company_name as client,d.supplier_name as supplier,e.cn_name as operator,f.title as own')
            ->join('category b','a.branch_id=b.id','left')           
            ->join('client c','a.client_id=c.id','left')
            ->join('supplier d','a.supplier_id=d.id','left')           
            ->join('user e','a.operator_id=e.id','left')
            ->join('category f','a.own_id=f.id','left')
            ->where('id',$id)->find(); 
        $singles['receipts'] = Db::name('receipt')->where(['type'=>$singles['type'],'income_id'=>$singles['id']])->select();  
        $singles['payments'] = Db::name('payment')->where(['type'=>$singles['type'],'outlay_id'=>$singles['id']])->select(); 
        return $singles;
    }

    public function infoBySn($sn){
        $singles = SingleModel::alias('a')
            ->field('a.*,b.title as branch,c.company_name as client,d.supplier_name as supplier,e.cn_name as operator,f.title as own')
            ->join('category b','a.branch_id=b.id','left')           
            ->join('client c','a.client_id=c.id','left')
            ->join('supplier d','a.supplier_id=d.id','left')           
            ->join('user e','a.operator_id=e.id','left')
            ->join('category f','a.own_id=f.id','left')
            ->where('sn',$sn)->select(); 
        foreach ($singles as &$v) {     
            $v['receipts'] = Db::name('receipt')->where(['type'=>$v['type'],'income_id'=>$v['id']])->select();          
            $v['payments'] = Db::name('payment')->where(['type'=>$v['type'],'outlay_id'=>$v['id']])->select(); 
        }
        return $singles;
    }

    public function save($params){              
        if(empty($params['id'])){            
            $data = SingleModel::create($params); 
        }else{
            $data = SingleModel::update($params, ['id' => $params['id']]); 
        }        
        return $data;
    }

    public function delete($params){   
        Db::startTrans();
        try {
            $single = Db::name('single')->where('id',$params['id'])->find();
            $receipt_data = Db::name('receipt')->where(["income_id"=>$single["id"],"type"=>$single['type']])->select();
            foreach($receipt_data as $r){
                Db::name('receipt')->where(["id"=>$r["id"],"type"=>$r['type']])->delete();
            }
            $payment_data = Db::name('payment')->where(["outlay_id"=>$single["id"],"type"=>$single['type']])->select();
            foreach($payment_data as $p){
                Db::name('payment')->where(["id"=>$p["id"],"type"=>$p['type']])->delete();
            } 
            SingleModel::destroy($params['id']);
            // 提交事务
            Db::commit();           
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
    }

    public function csv($params){
        $where=[];        
        if(!empty($params['typeid'])){
            $where[] = ['type','=',$params['typeid']];   
        }
        if(!empty($params['keyword'])){
            $where[] = ['content','like',"%{$params['keyword']}%"];   
        }
        $data = SingleModel::alias('a')
            ->field('a.*,b.title as branch,c.company_name as client,d.supplier_name as supplier,e.cn_name as operator,f.title as own')
            ->join('category b','a.branch_id=b.id','left')           
            ->join('client c','a.client_id=c.id','left')
            ->join('supplier d','a.supplier_id=d.id','left')           
            ->join('user e','a.operator_id=e.id','left')
            ->join('category f','a.own_id=f.id','left')
            ->where($where)->order('id','desc')->select();
        foreach ($data as &$v) {     
            $v['receipts'] = Db::name('receipt')->where(['type'=>$v['type'],'income_id'=>$v['id']])->select();          
            $v['payments'] = Db::name('payment')->where(['type'=>$v['type'],'outlay_id'=>$v['id']])->select(); 
        }
        return $data;
    } 
}